<template>
  <div>
    <el-form :model="form" :rules="rules" label-width="100px" ref="form">
      <el-form-item prop="title" label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item prop="typeId" label="类型">
        <el-select v-model="form.typeId" clearable>
          <el-option v-for="item in list" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="content" label="内容">
        <!-- <el-input type="textarea" v-model="form.content"></el-input> -->
        <Tiny v-model="form.content"></Tiny>
        {{ form.content }}
      </el-form-item>
      <el-form-item prop="cover" label="封面">
        <el-input v-model="form.cover"></el-input>
        <el-upload class="avatar-uploader" action="" :auto-upload="false" :show-file-list="false"
          :before-upload="beforeAvatarUpload" :on-change="handleCoverChange" name="img">
          <img v-if="form.cover" :src="form.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="publishDate" label="发布时间">
        <el-date-picker v-model="form.publishDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间" align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">保存</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { articleAdd } from '@/api/article';
import { uploadImg } from '@/api/common';
import Tiny from '@/components/Tiny';
export default {
  props: ["list", "info"],
  components: { Tiny },
  data() {
    return {
      form: {
        id: null,
        title: "",
        typeId: "",
        content: "",
        cover: "https://ts3.cn.mm.bing.net/th?id=OIP-C.8tlwbGsJvlNZwGAGL36W8AHaFS&w=295&h=211&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2",
        publishDate: "",
      },
      rules: {
        title: [{ required: true, trigger: 'blur', message: "请输入" }],
        typeId: [{ required: true, trigger: 'blur', message: "请选择" }],
        content: [{ required: true, trigger: 'blur', message: "请输入" }],
        cover: [{ required: true, trigger: 'blur', message: "请输入" }],
        publishDate: [{ required: true, trigger: 'blur', message: "请选择" }],
      }
    }
  },
  methods: {
    submit() {
      this.$refs["form"].validate(flag => {
        if (flag) {
          // console.log(this.form);
          articleAdd(this.form).then(res => {
            // console.log(res);
            this.$message({
              type: 'success',
              message: '保存成功!'
            });
            this.$emit("done")
          })
        }
      })
    },
    close() {
      this.$emit("done")
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG，PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!');
      }
      return isJPG && isLt2M;
    },
    handleCoverChange(file) {
      const formData = new FormData();
      formData.append("img", file.raw);
      uploadImg(formData).then(res => {
        // console.log(res);
        this.form.cover = res.data.img
      })
    }
  },
  mounted() {
    if (this.info.id) {
      this.form = JSON.parse(JSON.stringify(this.info))
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
